You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ng-style中的图片URL作为参数传入saveData函数?

Solution: Pass the Image URL to saveData()

There are two straightforward ways to pass that constructed image URL to your saveData() function. Let's break them down:

Approach 1: Directly Pass the Constructed URL in ng-click

Since the variables used to build the URL (prefixVal, completiondetail.IDProofURL, toaken) are already available in your scope, you can assemble them directly in the ng-click expression:

Update the Save Button

<input type="button" name="save" ng-click="saveData(prefixVal + completiondetail.IDProofURL + toaken)" value="save">

Modify Your Controller's saveData Function

Make sure the function accepts the URL parameter:

$scope.saveData = function(imageUrl) {
  // Now you can use the imageUrl parameter for your save logic
  console.log('Image URL to save:', imageUrl);
  // ... rest of your save code
};

This is a quick fix that works if you don't need to reuse the URL elsewhere in your template or controller.

To avoid repeating the URL construction in both ng-style and ng-click, store the full URL in a scope variable. This follows the DRY (Don't Repeat Yourself) principle and makes future changes easier.

Step 1: Compute and Watch the URL in Your Controller

// Initialize the image URL
$scope.imageUrl = $scope.prefixVal + $scope.completiondetail.IDProofURL + $scope.toaken;

// Watch for changes to the underlying variables and update the URL automatically
$scope.$watchGroup(['prefixVal', 'completiondetail.IDProofURL', 'toaken'], function(newValues) {
  // newValues[0] = prefixVal, newValues[1] = completiondetail.IDProofURL, newValues[2] = toaken
  $scope.imageUrl = newValues[0] + newValues[1] + newValues[2];
});

Step 2: Update the Modal Container's ng-style

Use the new scope variable instead of constructing the URL inline:

<div class="modalContainer" ng-style="{'background-image': 'url(' + imageUrl +')'}">
</div>

Step 3: Pass the Variable to saveData()

Update the save button to use the precomputed URL:

<input type="button" name="save" ng-click="saveData(imageUrl)" value="save">

Controller's saveData Function (Same as Approach 1)

$scope.saveData = function(imageUrl) {
  // Use imageUrl for your save operations
  console.log('Saving image URL:', imageUrl);
};

This approach is better because if you ever need to adjust how the URL is built (like adding a new parameter or changing the order), you only have to update the controller code once instead of modifying both the template's ng-style and ng-click.

内容的提问来源于stack exchange,提问作者sathish kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:19:48