Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angular js指令未从web服务加载数组_Javascript_Angularjs - Fatal编程技术网

Javascript angular js指令未从web服务加载数组

Javascript angular js指令未从web服务加载数组,javascript,angularjs,Javascript,Angularjs,我试图将数组链接到指令,但数据没有显示。当页面加载时,数据通过webservice调用进入: var profile = angular.module('profile.controllers', []); profile.directive('propertyRow', function() { return { restrict: 'AE', replace: true, templateUrl: '/partials/propRo

我试图将数组链接到指令,但数据没有显示。当页面加载时,数据通过webservice调用进入:

var profile = angular.module('profile.controllers', []);


profile.directive('propertyRow', function() {
    return {
        restrict: 'AE',
        replace: true,
        templateUrl: '/partials/propRow.ejs',
        scope: {
            displayProps : "="
        },
        link : function(scope, element, attrs) {
            element.bind('click', function() {
                console.log('clicked a property');
            });
        }
    };
});


profile.controller('profileController', ['$scope', '$http', '$window',  function($scope, $http, $window){
    var landlord_id = my_user;
    $scope.properties = [];
    $scope.landlord_id = landlord_id

    $http.get('/api/getPropsByLandlord/' + landlord_id)
        .success(function(data) {
            $scope.properties = data;
        })

指令的html:

<property-row display-props="properties"></property-row>

模板:

<tr ng-repeat="property in displayProps">
  <td>{{property.name}}</td> 
</tr> 

{{property.name}

如果模板是
hello

,那么它工作正常,因此我假设它与负载有关。

因此,看起来我最初关于控制器和指令之间的链接的假设是错误的。已正确完成绑定到控制器的范围值(
displayProps
->
属性
)。问题似乎在于页面加载和指令加载之间的延迟条件。你对装载量的推测可能是正确的

将控制器更改为以下内容:

profile.controller('profileController', ['$scope', '$http', '$window',  function($scope, $http, $window){
 var landlord_id = my_user;
    $scope.properties = [];
    $scope.landlord_id = landlord_id

    $http.get('/api/getPropsByLandlord/' + landlord_id)
        .success(function(data) {
            $scope.properties = data;
        })

    $scope.$watch('properties', function(val) {
      $scope.properties = val;
    })
}]);
它应该会起作用。以这个plunkr为例:

我没有复制您的http请求,因为我相信它不是问题的根源。Angular的摘要循环加载页面,然后加载指令。如果指令作用域中有一个值,它将不会被第一个摘要循环捕获,因此您必须观察该值,并在它更改时触发一个新的摘要循环


在此处阅读有关angular的摘要循环的更多信息:

我需要使用作用域。$apply以确保我的指令正在接收数据

$.get('/api/getPropsByLandlord/' + my_user)
                .success(function(data) {
                    $scope.$apply(function () {
                        $scope.properties = data;
                    });
                })

看起来您尚未将控制器的作用域连接到指令的作用域。尝试按照此处列出的控制器定义进行操作:@Oleg似乎正在将其指令的本地作用域值
displayProps
设置为控制器中的
properties
模型。他所缺少的只是两者之间的联系。“我正在整理一个完整的答案。”多帕塔曼似乎是这样。去做吧,一切看起来都应该对我有用。您确定get请求没有产生错误吗?尝试将.error处理程序放在.success处理程序之后。它不是。。。我有一个错误处理程序,我记录了成功。。。都很好。啊,那也行。问题的根源是完成第一个$digest和加载指令数据之间的延迟。干得好。