2
我只是在做這個教程,這很有道理 - http://onehungrymind.com/angularjs-sticky-notes-pt-2-isolated-scope/。提供的小提琴在這裏:http://jsfiddle.net/simpulton/SPMfT/角度「控制器作爲語法」,傳遞變量,並隔離範圍
它顯示瞭如何使用@,=,&將屬性綁定到父控制器作用域。
我想改變用「控制器作爲語法」小提琴,但似乎無法得到它的工作,我的小提琴是在這裏 - http://jsfiddle.net/SPMfT/304/
爲什麼這是行不通的任何想法?
查看:
<div ng-controller="MyCtrl as ctrl">
<h2>Parent Scope</h2>
<input ng-model="ctrl.foo"> <i>// Update to see how parent scope interacts with component scope</i>
<br><br>
<!-- attribute-foo binds to a DOM attribute which is always
a string. That is why we are wrapping it in curly braces so
that it can be interpolated.
-->
<my-component attribute-foo="{{ctrl.foo}}" binding-foo="ctrl.foo"
isolated-expression-foo="ctrl.updateFoo(newFoo)" >
<h2>Attribute</h2>
<div>
<strong>get:</strong> {{isolatedAttributeFoo}}
</div>
<div>
<strong>set:</strong> <input ng-model="isolatedAttributeFoo">
<i>// This does not update the parent scope.</i>
</div>
<h2>Binding</h2>
<div>
<strong>get:</strong> {{isolatedBindingFoo}}
</div>
<div>
<strong>set:</strong> <input ng-model="isolatedBindingFoo">
<i>// This does update the parent scope.</i>
</div>
<h2>Expression</h2>
<div>
<input ng-model="isolatedFoo">
<button class="btn" ng-click="isolatedExpressionFoo({newFoo:isolatedFoo})">Submit</button>
<i>// And this calls a function on the parent scope.</i>
</div>
</my-component>
</div>
JS:
var myModule = angular.module('myModule', [])
.directive('myComponent', function() {
return {
restrict:'E',
scope:{
/* NOTE: Normally I would set my attributes and bindings
to be the same name but I wanted to delineate between
parent and isolated scope. */
isolatedAttributeFoo:'@attributeFoo',
isolatedBindingFoo:'=bindingFoo',
isolatedExpressionFoo:'&'
}
};
})
.controller('MyCtrl', ['$scope', function ($scope) {
this.foo = 'Hello!';
var self = this;
this.updateFoo = function (newFoo) {
self.foo = newFoo;
}
}]);
你肯定你的提琴不使用控制器的語法,即使工作?它似乎並沒有爲我工作 – JoseM 2014-10-10 22:25:00
雅這個版本不使用控制器作爲語法 - http://jsfiddle.net/simpulton/SPMfT/第一個指令是綁定與@第二個是2路綁定=。第三個使用&在父控制器上調用一個函數。 – 2014-10-11 03:56:29
該小提琴使用過時版本的角度,不支持控制器作爲語法。看到下面的小提琴,我剛剛改變角度的版本是最新的,你看它如何不工作:http://jsfiddle.net/SPMfT/306/ – JoseM 2014-10-12 15:54:18