angular js$scope变量与字符串绑定
angular js $scope variable binding with string
Angular没有意识到,当我把model(string)和$scope放在一起形成$scope.model时,它确实是一个定义的范围变量。有没有办法不让model成为字符串,这样$scope.model就会真正引用我需要它引用的内容?
$scope.games = [];
$scope.maxplayers = 0;
$scope.minplayers = 0;
........
function setupWatcher(model){
$scope.$watch(model,function(){
console.log($scope.model); // prints out undefined
if(!$scope.model) return;
$http.get('/api/games?'+ model + '=' + $scope.model)
.success(function(data){
$scope.games = data;
})
.error(function(data){
console.log('Error' + data);
});
});
}
angular.forEach(['maxplayers',' minplayers'],setupWatcher);
使用括号表示法:
$scope[model]
是的,你应该这样做
$scope[model]
您可以通过任一方式访问对象属性
var propName = 'prop3',
myObject = {
prop1: "123",
prop2: "456",
prop3: "789"
};
myObject.prop1 // 123
myObject['prop2'] // 456
myObject[propName] // 789
相关文章:
- 将敲除绑定应用于模板,并将结果作为字符串获取
- 我们可以将ng绑定值存储到php字符串中吗
- 挖空 HTML 绑定和字符串格式的 HTML 长度
- Popover未将字符串绑定为html angularjs
- angular js$scope变量与字符串绑定
- angular2中具有属性绑定的字符串串联
- 挖空 JS html 绑定返回奇怪的代码而不是 html 字符串
- 从链接内的数据绑定中获取字符串
- 使用字符串键绑定嵌套对象
- 如何在绑定到使用 ngBindHTML 查看时缩短 HTML 字符串的内容文本
- 如何在 angularjs 中绑定为字符串的 html 部分
- AngularJS - 需要帮助在绑定文本输入中显示字符串的当前长度
- 如何在 javascript src 字符串中插入 ASP.NET 数据绑定器
- 剑道绑定属性与字符串和变量
- 双向绑定 ng-repeat 中的字符串数组
- SAPUI5/OPENUI5:将字符串绑定到 JSON 模型并自动更新更大的字符串(读取 [对象%20Object])
- 如何使用 AngularJS 绑定后获取元素的字符串长度
- 敲除.js如何从绑定构建字符串
- AngularJS -从JSON字符串绑定数据
- 将部分字符串绑定到Angular作用域变量