Angularjs 本地/父数据绑定赢得';不使用指令隔离范围
我试图在定义指令时使用Angularjs 本地/父数据绑定赢得';不使用指令隔离范围,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我试图在定义指令时使用scope:{test:=“}符号实现本地/父数据绑定 然而,在下面的示例中,尽管console.log($scope.$parent)记录父作用域确实定义了一个属性test,此代码引发异常与指令“testdirective”一起使用的表达式“undefined”是不可赋值的 如果我使用范围:{test:=?“},则不会再引发异常,但是在父指令和指令$scope.test之间没有数据绑定 TestCtrl是视图的控制器 我做错了什么 app.js var ng_app =
scope:{test:=“}
符号实现本地/父数据绑定
然而,在下面的示例中,尽管console.log($scope.$parent)
记录父作用域确实定义了一个属性test
,此代码引发异常与指令“testdirective”一起使用的表达式“undefined”是不可赋值的
如果我使用范围:{test:=?“}
,则不会再引发异常,但是在父指令和指令$scope.test
之间没有数据绑定
TestCtrl是视图的控制器
我做错了什么
app.js
var ng_app = angular.module('my_app',['ngRoute']);
ng_app.config(['$routeProvider', function($routeProvider){
$routeProvider.
when('/testroute/', {templateUrl: 'partials/test.html', controller:'TestCtrl'}).
otherwise({redirectTo: '/testroute/'});
}]);
ng_app.controller('TestCtrl', function($scope){$scope.test = 0;});
ng_app.directive('testdirective', function()
{
return {
restrict:'E',
scope: {test:"="}, // this complains that the parent.scope has no property 'test'
controller: function($scope){
// this logs a scope with adequate 'test' property defined:
console.log($scope.$parent);
$scope.test = 1;
}
}
});
partials/test.html
<testdirective></testdirective>
我使用angular 1.2.0rc3是的,作用域:{test:=“}
不定义您想要从父作用域中得到什么。它定义了连接到封闭范围的属性
因此,您需要在声明中添加一个名为test
的属性:
<testdirective test="test"></testdirective>
<testdirective myModel="test"></testdirective>
声明如下:
<testdirective test="test"></testdirective>
<testdirective myModel="test"></testdirective>
hmm我想你说的是用{test:@}完成的。如果我引用API文档:@或@attr-将本地作用域属性绑定到DOM属性的值or=attr-在本地作用域属性和父作用域属性之间设置双向绑定,名称作为attr属性的值。@不创建双向绑定。因此,如果您只想将值绑定到指令范围中,请使用@。否则,如果要更改指令内的值并使值“bind back out”使用=。