小编use*_*994的帖子

如何使用angularJS正确绑定指令和控制器之间的范围

我正在尝试使用anugularJS生成一个n级分层无序列表,并且已经能够成功完成.但是现在,我在指令和控制器之间存在范围问题.我需要在指令模板中通过ng-click调用的函数中更改父类的scope属性.

请参阅http://jsfiddle.net/ahonaker/ADukg/2046/ - 这是JS

var app = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.itemselected = "None";
    $scope.organizations = {
        "_id": "SEC Power Generation",
        "Entity": "OPUNITS",
        "EntityIDAttribute": "OPUNIT_SEQ_ID",
        "EntityID": 2,
        "descendants": ["Eastern Conf Business Unit", "Western Conf Business Unit", "Atlanta", "Sewanee"],
        children: [{
            "_id": "Eastern Conf Business Unit",
            "Entity": "",
            "EntityIDAttribute": "",
            "EntityID": null,
            "parent": "SEC Power Generation",
            "descendants": ["Lexington", "Columbia", "Knoxville", "Nashville"],
            children: [{
                "_id": "Lexington",
                "Entity": "OPUNITS",
                "EntityIDAttribute": "OPUNIT_SEQ_ID",
                "EntityID": 10,
                "parent": "Eastern …
Run Code Online (Sandbox Code Playgroud)

scope using-directives hierarchy angularjs

18
推荐指数
2
解决办法
4万
查看次数

如何动态定义要在AngularJS指令模板中使用ng-click调用的函数

我正在尝试根据模型动态生成表单输入和相关的操作菜单.我能够传递要使用的字段和菜单,但我无法弄清楚如何配置ng-click来调用模型中定义的相应函数.请参阅小提琴:http: //jsfiddle.net/ahonaker/nkuDW/

HTML:

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

myApp.directive('myDirective', function($compile) {
    return {
        restrict: "E",
        replace: true,
        scope : {
            field: '=',
            label: '=',
            menu: '='
        },
        link:  function (scope, element, attrs) {
            element.html('{{label}}: <input ng-model="field"> <ul ng-repeat="item in menu"<li><a ng-click="item.func">{{item.title}}</a></li></ul>');
             $compile(element.contents())(scope);
        }
    }
});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.status = 'You have not picked yet';

    $scope.menu = [
        { "title" : "Action 1", "func" : "ActionOne()"},
        { "title" : "Action 2", "func" : "ActionTwo()"},
        ]

    $scope.fieldOne …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

9
推荐指数
1
解决办法
2万
查看次数