相关疑难解决方法(0)

使用ng-include时失去范围

我有这个模块路线:

var mainModule = angular.module('lpConnect', []).
    config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/home', {template:'views/home.html', controller:HomeCtrl}).
        when('/admin', {template:'views/admin.html', controller:AdminCtrl}).
        otherwise({redirectTo:'/connect'});
}]);
Run Code Online (Sandbox Code Playgroud)

主页HTML:

<div ng-include src="views.partial1"></div>
Run Code Online (Sandbox Code Playgroud)

partial1 HTML:

<form ng-submit="addLine()">
    <input type="text" ng-model="lineText" size="30" placeholder="Type your message here">
</form>
Run Code Online (Sandbox Code Playgroud)

HomeCtrl:

function HomeCtrl($scope, $location, $window, $http, Common) {
    ...
    $scope.views = {
        partial1:"views/partial1.html"
    };

    $scope.addLine = function () {
        $scope.chat.addLine($scope.lineText);
        $scope.lines.push({text:$scope.lineText});
        $scope.lineText = "";
    };
...
}
Run Code Online (Sandbox Code Playgroud)

addLine函数$scope.lineTextundefined,这可以通过添加ng-controller="HomeCtrl"来解决partial1.html,但是它会导致控制器被调用两次.我在这里错过了什么?

html javascript angularjs angularjs-scope angularjs-ng-include

180
推荐指数
3
解决办法
11万
查看次数

AngularJs:为什么异步数据到达时我的指令中的范围不会更新?

我有一个带有隔离范围的指令,它通过引用获取范围变量

angular.module('myApp')
    .directive('myDirective', function() {
        return {
            scope: {
                items: '='
            },
            templateUrl: 'template.html',
            replace: true,
            controller: 'myDirectiveCtrl',
            controllerAs: 'ctrl'
        };
    })
    .controller('myDirectiveCtrl', function($scope) {
        this.items = $scope.items;
    });
Run Code Online (Sandbox Code Playgroud)

传递方式如下:

    <div my-directive items='items'></div>
Run Code Online (Sandbox Code Playgroud)

在外部控制器中,数据被异步加载,并且传递给指令的范围项更新:

angular.module('myApp', [])
  .controller('myCtrl', function($scope) {

    $scope.setItems = function() {
      $scope.items = [
        'Here',
        'There',
        'Everywhere'
      ];
    };
  });
Run Code Online (Sandbox Code Playgroud)

加载数据时,我的指令范围之外的范围会更新,但内部则不会

我的HTML:

      <div my-directive items='items'></div> <!-- this doesn't update --> 

      Outside directive
      <ul ng-repeat='i in items'>            <!-- this does update -->
        <li>{{i}}</lu>
      </ul>

      <button ng-click="setItems()">Set items</button>
Run Code Online (Sandbox Code Playgroud)

如何在我的指令中更新我的范围?我

这里的人物

angularjs angularjs-directive angularjs-scope

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