//Here is the HTML code
<div ng-controller="getuserById" userid="{{ques.created_by}}" >
</div>
//Here is the Controller
obj.controller('getuserById', function ($scope, $http,$attrs) {
console.log($attrs.userid);
$http.get("some_url_here?attr1="+$attrs.userid)
.success(function(data){
$scope.user = data;
})
.error(function() {
$scope.user = "error in fetching data";
});
});
从上面的代码中,当我将硬代码放在userid属性中然后它在动态数据中工作时它不会 那么告诉我如何解决它?
答案 0 :(得分:0)
用户ng-model代替。这是修改后的代码。
<div ng-controller="getuserById" ng-init="userid=ques.created_by" >
</div>
使用Javascript:
obj.controller('getuserById', function ($scope, $http) {
$http.get("some_url_here?attr1="+$scope.userid)
.success(function(data){
$scope.user = data;
})
.error(function() {
$scope.user = "error in fetching data";
});
});
答案 1 :(得分:0)
//这是HTML
<div getuserbyid value="{{ques.created_by}}" > </div>
//这是我在下面使用的指令
obj.directive('getuserbyid', function ($http) {
return function (scope, element, attr) {
attr.$observe('value', function(actual_value) {
element.val("value = "+ actual_value);
})
console.log(attr.value);
$http.get("some_url_here?attr1="+attr.value)
.success(function(data){
scope.user = data;
})
.error(function() {
scope.user = "error in fetching data";
});
}
});
//上面的代码工作正常:)