Javascript 带传递参数的角度数据绑定
继续我的项目,但在数据绑定方面遇到了问题 我的数据中的更改没有反映在DOM上 我在JS函数/对象中有一个构造函数Javascript 带传递参数的角度数据绑定,javascript,angularjs,Javascript,Angularjs,继续我的项目,但在数据绑定方面遇到了问题 我的数据中的更改没有反映在DOM上 我在JS函数/对象中有一个构造函数 constructor: function() { var self = this; var navigation = angular.module("navigation", []); navigation.controller("ProductIDsController", function($scope) { $scope.product
constructor: function() {
var self = this;
var navigation = angular.module("navigation", []);
navigation.controller("ProductIDsController", function($scope) {
$scope.productIds = self.productIds;
});
angular.bootstrap(document.getElementById('navigation'), ['navigation']);
}
和产品id在同一级别上定义
productIds: ["abc", "xyz", "test"], //DOM gets populated from this array via angular
init: function(productIds) {
console.log(this.productIds); // displays ["abc", "xyz", "test"]
this.productIds.push("another item"); //this didn't work on the dom either
this.productIds = productIds; //I changed the productId's by passing another array
console.log(this.productIds); //the array got changed but DOM is still the same,
}
HTML
- {{productId}
最初,DOM由给定的数组填充,但在传入另一个数组后,它不会改变。如何在给定场景中绑定productId的数据
编辑:jsfiddle
我认为最好的方法是使用服务来广播ProductID中的更改
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
链接到服务的控制器:
navigation.controller("ProductIDsController", ['ProductSvc'],
function($scope, productSvc) {
...
$scope.$on('changeIds', function() {
$scope.productIds = ProductSvc.productIds;
})
}
通过服务更改productId(例如在处理用户输入的控制器中)
我认为最好的方法是使用服务来广播ProductID中的更改
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
链接到服务的控制器:
navigation.controller("ProductIDsController", ['ProductSvc'],
function($scope, productSvc) {
...
$scope.$on('changeIds', function() {
$scope.productIds = ProductSvc.productIds;
})
}
通过服务更改productId(例如在处理用户输入的控制器中)
我认为最好的方法是使用服务来广播ProductID中的更改
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
链接到服务的控制器:
navigation.controller("ProductIDsController", ['ProductSvc'],
function($scope, productSvc) {
...
$scope.$on('changeIds', function() {
$scope.productIds = ProductSvc.productIds;
})
}
通过服务更改productId(例如在处理用户输入的控制器中)
我认为最好的方法是使用服务来广播ProductID中的更改
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
链接到服务的控制器:
navigation.controller("ProductIDsController", ['ProductSvc'],
function($scope, productSvc) {
...
$scope.$on('changeIds', function() {
$scope.productIds = ProductSvc.productIds;
})
}
通过服务更改productId(例如在处理用户输入的控制器中)
在范围外更改值时,必须显式调用
$scope.$apply()
。
更新
在范围外更改值时,必须显式调用
$scope.$apply()
。
更新
在范围外更改值时,必须显式调用
$scope.$apply()
。
更新
在范围外更改值时,必须显式调用
$scope.$apply()
。
更新
您遇到了一个典型的Javascript错误,与AngularJS无关。在Javascript中,数组和对象总是通过引用传递。假设如下:
this.productIds = [1,2,3]
$scope.productIds = this.productIds;
这将形成一个数组,我们称之为“A”。然后,它将在this.productId和$scope.productId中放置对该数组的引用
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
如果您现在这样做:
this.productIds = [4,5,6];
console.log(this.productIds);
console.log($scope.productIds);
然后你会得到:
[4,5,6]
[1,2,3]
为什么??因为设置this.productId
不会更改数组。它将更改此.productId指向的数组
解决这个问题有很多选择。这里有一个快速破解:
this.productIds.length = 0; // Truncate the existing array instead of making a new one
angular.forEach(productIds, function(entry) {
this.productIds.push(entry); // Copy the elements into our existing array
});
这不一定是最有效的,但它会准确地向您显示发生了什么。您遇到了一个典型的Javascript错误,与AngularJS无关。在Javascript中,数组和对象总是通过引用传递。假设如下:
this.productIds = [1,2,3]
$scope.productIds = this.productIds;
这将形成一个数组,我们称之为“A”。然后,它将在this.productId和$scope.productId中放置对该数组的引用
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
如果您现在这样做:
this.productIds = [4,5,6];
console.log(this.productIds);
console.log($scope.productIds);
然后你会得到:
[4,5,6]
[1,2,3]
为什么??因为设置this.productId
不会更改数组。它将更改此.productId指向的数组
解决这个问题有很多选择。这里有一个快速破解:
this.productIds.length = 0; // Truncate the existing array instead of making a new one
angular.forEach(productIds, function(entry) {
this.productIds.push(entry); // Copy the elements into our existing array
});
这不一定是最有效的,但它会准确地向您显示发生了什么。您遇到了一个典型的Javascript错误,与AngularJS无关。在Javascript中,数组和对象总是通过引用传递。假设如下:
this.productIds = [1,2,3]
$scope.productIds = this.productIds;
这将形成一个数组,我们称之为“A”。然后,它将在this.productId和$scope.productId中放置对该数组的引用
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
如果您现在这样做:
this.productIds = [4,5,6];
console.log(this.productIds);
console.log($scope.productIds);
然后你会得到:
[4,5,6]
[1,2,3]
为什么??因为设置this.productId
不会更改数组。它将更改此.productId指向的数组
解决这个问题有很多选择。这里有一个快速破解:
this.productIds.length = 0; // Truncate the existing array instead of making a new one
angular.forEach(productIds, function(entry) {
this.productIds.push(entry); // Copy the elements into our existing array
});
这不一定是最有效的,但它会准确地向您显示发生了什么。您遇到了一个典型的Javascript错误,与AngularJS无关。在Javascript中,数组和对象总是通过引用传递。假设如下:
this.productIds = [1,2,3]
$scope.productIds = this.productIds;
这将形成一个数组,我们称之为“A”。然后,它将在this.productId和$scope.productId中放置对该数组的引用
navigation.factory('ProductSvc',
['$rootScope', function($rootScope) {
return {
productIds : [],
changeProductIds: function(newIds){
this.productIds = newIds
$rootScope.$broadcast('changeIds');
}
}
}])
如果您现在这样做:
this.productIds = [4,5,6];
console.log(this.productIds);
console.log($scope.productIds);
然后你会得到:
[4,5,6]
[1,2,3]
为什么??因为设置this.productId
不会更改数组。它将更改此.productId指向的数组
解决这个问题有很多选择。这里有一个快速破解:
this.productIds.length = 0; // Truncate the existing array instead of making a new one
angular.forEach(productIds, function(entry) {
this.productIds.push(entry); // Copy the elements into our existing array
});
这不一定是最有效的,但它会准确地向您显示正在发生的事情。它没有被更改,因为productsIds数组不在角度范围内。我猜数组是通过引用传递的,数组中的任何更改都应该反映在那里。它没有被更改,因为productsIds数组在角度范围之外。我猜数组是通过引用传递的,数组中的任何更改都应该反映在那里。它没有被更改,因为productsIds数组在角度范围之外。我猜数组是通过引用传递的,数组中的任何更改都应该反映在那里它没有被更改,因为productsIds数组不在角度范围内。我猜数组是通过引用传递的,数组中的任何更改都应该反映在那里抱歉,我不熟悉角度,在上面的场景中,你能帮助我写这行代码的位置和方式吗?你有JSFIDLE吗?更好的方法是将在角度范围之外进行更改的代码包装在$timeout中,不设置间隔。这是一种推荐的方法,可以在范围外的更改后以安全的方式运行摘要。@Kechol很抱歉,一个(我不是这样做的吗