AngularJS - 数据绑定到方法与属性

时间:2015-09-29 13:10:54

标签: javascript html angularjs

请问您解释为什么方法1(绑定到属性)不起作用,而方法2(绑定到函数返回值)有效吗?

在此处包含相关代码。如果您需要任何其他代码,请发表评论。

function MyController(myService) {
    // Method 1: A property
    this.backgroundImagePath = login.backgroundImagePath;

    // Method 2: A function
    this.getBackgroundImagePath = function () {
        return login.backgroundImagePath;
    };
}

然后在我的HTML代码中,我有:

<!-- Does not work -->
<img data-ng-src="{{ myController.backgroundImagePath }}" />

<!-- Works as expected -->
<img data-ng-src="{{ myController.getBackgroundImagePath() }}" />

0 个答案:

没有答案