相关疑难解决方法(0)

我可以将服务注入指令吗?

我正在尝试将服务注入到如下的指令中:

 var app = angular.module('app',[]);
 app.factory('myData', function(){
     return {
        name : "myName"
     }
 });
 app.directive('changeIt',function($compile, myData){
    return {
            restrict: 'C',
            link: function (scope, element, attrs) {
                scope.name = myData.name;
            }
        }
 });
Run Code Online (Sandbox Code Playgroud)

但这让我错了Unknown provider: myDataProvider.有人可以查看代码并告诉我,如果我做错了什么?

angularjs

233
推荐指数
3
解决办法
18万
查看次数

AngularJS - 将函数传递给指令

我有一个例子angularJS

<div ng-controller="testCtrl">

<test color1="color1" updateFn="updateFn()"></test>
</div>
 <script>
  angular.module('dr', [])
.controller("testCtrl", function($scope) {
    $scope.color1 = "color";
    $scope.updateFn = function() {
        alert('123');
    }
})
.directive('test', function() {
    return {
        restrict: 'E',
        scope: {color1: '=',
                updateFn: '&'},
        template: "<button ng-click='updateFn()'>Click</button>",
        replace: true,
        link: function(scope, elm, attrs) { 
        }
    }
});

</script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

我想当我点击按钮时,会出现警告框,但没有任何显示.

谁能帮我?

angularjs

156
推荐指数
5
解决办法
14万
查看次数

标签 统计

angularjs ×2