Angularjs:在指令中创建一个新的scope属性

时间:2016-06-04 21:34:48

标签: angularjs angularjs-directive

该值永远不会显示在DOM上,我只是想看看这种方式是否有效..

我要做的是在指令中创建一个新的范围值并将其显示给DOM

click

我试图显示的数据来自一个返回一个promise的函数,并且带有promise我希望在DOM中使用的数据

HTML的编码如下:

app.directive("rest", ["restFactory", function($rest){
    return {
        restrict: "A",
        scope: {
            uri: "@rest",
        },
        link: function(scope){
            scope.$rest = [];

            $rest.batch(scope.uri)
                .then(function(data){
                    scope.$rest = data;
                });
        }
    }
}]);

这是我做过的第一个指令。

1 个答案:

答案 0 :(得分:1)

我让this plunker解释了为什么你的指令无效。

<div rest="accounts">
    <!-- You can't access the directive variable $rest from here 
         The directives variables are only available on it's template. -->
    <div ng-repeat="data in $rest">
        {{data.id}}
    </div>
</div>


这将有效:

app.directive("restTwo", function() {
    return {
        restrict: "A",
        scope: {},
        // It will work. I put the template in here.
        // You can only access the directive variables from it's template
        template: '<div ng-repeat="data in $rest">{{data.id}}</div>',

        link: function(scope, el, attr) {
            console.log(attr.rest); // output = accounts

            //data
            scope.$rest = [{
                'id': 1,
                'name': 'John Doe'
            }, {
                'id': 2,
                'name': 'Johana Doe'
            }];
            console.log(scope.$rest);
        }
    }
});


我建议你建一个工厂,然后像这样打电话给你:

app.factory('myFactory', function($http) {

    // GET example
    this.get = function(string) {
        return $http({
            method: 'GET',
            url: 'https://api.github.com/search/repositories?q=' + string
        });
    }

    // Your request here
    this.yourRequest = function(uri) {
        // return $rest.batch(uri);
    }

    return this;
});

在你的控制器中:

app.controller('MainCtrl', function($scope, myFactory) {
    $scope.myData = [];


    myFactory.get('tetris').then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.myData = response.data.items;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

});

查看:

<div ng-repeat="data in myData">
    {{data.id}}
</div>


如果您真的想要使用指令(我不建议):
指令:

app.directive("restThree", function() {
    return {
        restrict: "A",
        scope: {
            'data': '='
        },
        link: function(scope, el, attr) {
            //console.log(attr.rest); // output = accounts

            //data
            scope.$rest = [{
                'id': 1,
                'name': 'John Doe'
            }, {
                'id': 2,
                'name': 'Johana Doe'
            }];

            scope.data = scope.$rest;
        }
    }
});

查看:

<div rest-three="accounts" data="directiveData">
    <div ng-repeat="data in directiveData">
        {{data.id}}
    </div>
</div>